//
//  SQLiteManager.m
//  homeWork-18
//
//  Created by 鹏飞罗 on 16/8/8.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "SQLiteManager.h"
#import "Person.h"
@implementation SQLiteManager
+ (SQLiteManager *)sharedManger{
    static SQLiteManager *manager = nil;
    // 保证线程安全, 只执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SQLiteManager alloc] init];
    });
    return manager;
}

- (BOOL)openSQLite3 {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [pathArray firstObject];
    
    NSString *dbPath = [document stringByAppendingPathComponent:@"person.db"];
    NSLog(@"%@", dbPath);
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    return [self isOperationResult:result alertText:@"创建数据库"];
}

- (BOOL)closeSQLite3 {
   int result = sqlite3_close(dbPoint);
    return [self isOperationResult:result alertText:@"关闭数据库"];
}
//创建表
- (BOOL)cerateTable {
    NSString *createTableSQL = @"create table person(person_id integer primary key autoincrement,person_image text, person_name text not null ,person_phoneNumber text not null,person_address text)";
    int result = sqlite3_exec(dbPoint, [createTableSQL UTF8String], NULL, NULL, NULL);
    return [self isOperationResult:result alertText:@"创建表"];

    
    return YES;
}
// 插入数据
- (BOOL)insertPerson:(Person *)person {
    NSString *insertSQL = [NSString stringWithFormat:@"insert into person values (null, '%@', '%@', '%@', '%@')",person.personImagePath, person.name, person.phoneNumber, person.address];
    
   int result = sqlite3_exec(dbPoint, [insertSQL UTF8String], NULL, NULL, NULL);
    return [self isOperationResult:result alertText:@"添加数据"];
}

- (__kindof NSArray *)selectAll {
    NSString *selectAllSQL = @"select * from person";
    sqlite3_stmt *stmt = NULL;
   int result = sqlite3_prepare(dbPoint, [selectAllSQL UTF8String], -1, &stmt, NULL);
    NSMutableArray *personArray = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            Person *person = [[Person alloc] init];
            const unsigned char *imagePath = sqlite3_column_text(stmt, 1);
            person.personImagePath = [NSString stringWithUTF8String:(const char *)imagePath];
//            stu.stu_name = [NSString stringWithUTF8String:(const char *)stu_name];
            const unsigned char *personName = sqlite3_column_text(stmt, 2);
            person.name = [NSString stringWithUTF8String:(const char *)personName];
            const unsigned char *personPhoneNumber = sqlite3_column_text(stmt, 3);
            person.phoneNumber = [NSString stringWithUTF8String:(const char *)personPhoneNumber];
            const unsigned char *personAddress = sqlite3_column_text(stmt, 4);
            person.address = [NSString stringWithUTF8String:(const char *)personAddress];
            [personArray addObject:person];
        }
    }
    sqlite3_finalize(stmt);

    return personArray;
}

- (BOOL)isOperationResult:(int)result alertText:(NSString *)text{
    BOOL isOperation = YES;
    if (result == SQLITE_OK) {
        NSLog(@"%@成功", text);
        isOperation = YES;
    } else {
        NSLog(@"%@失败------%d", text, result);
        isOperation = NO;
    }
    return isOperation;
}
@end
